<template>
  <view class="my-login">
    <uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
    <!-- <button class="btn" type="warn" open-type="getUserInfo" @getuserinfo="getInfo">一键登录</button> -->
    <button class="btn" type="warn" @click="getInfo2">一键登录</button>
    <!--    <button class="btn" type="warn" open-type="chooseAvatar" @chooseavatar="getAvatar">更换头像</button> -->


    <view class="text">
      登录后尽享更多权益
    </view>
  </view>
</template>

<script>
  export default {
    name: "my-login",
    data() {
      return {

      };
    },
    methods: {
      getInfo(res) {
        console.log('getUserInfo用户信息', res);
      },
      async getInfo2() {
        const res = await uni.getUserProfile({
          desc: '展示用户信息'
        })
        this.$store.commit('user/setUserInfo', res.userInfo)
        // 获取token
        const loginRes = await uni.login()
        const res2 = await uni.$http.post('/users/wxlogin', {
          data: {
            code: loginRes.code,
            encryptedData: res.encryptedData,
            rawData: res.rawData,
            iv: res.iv,
            signature: res.signature
          }
        })
        // 存储token
        this.$store.commit('user/setToken', res2.message.token)
        // 如果有回跳地址，应该跳转至回路地址
        if (this.$store.state.user.backPath.url) {
          // 跳转至来的页面，
          uni[this.$store.state.user.backPath.openType]({
            url: this.$store.state.user.backPath.url
          })
          // 使用完成清理相应数据
          this.$store.commit('user/setBackPath', {})
          // if (this.$store.state.backPath.openType === 'switchTab') {
          //   uni.switchTab({
          //     url: this.$store.state.backPath.url
          //   })
          // } else {
          //   uni.navigateTo({
          //     url: this.$store.state.backPath.url
          //   })
          // }
        }
        console.log('登录', res2);


      },
      getAvatar(res) {
        console.log('获取用户头像地址', res);
      }
    }
  }
</script>

<style lang="scss" scoped>
  .my-login {
    padding: 160rpx 20rpx;
    text-align: center;

    .btn {
      background-color: #c00000;
      border-radius: 40rpx;
      margin-top: 40rpx;
    }

    .text {
      margin-top: 40rpx;
      color: #afafaf;
      font-size: 12px;
    }
  }
</style>